
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

extern struct rt_semaphore g_sem_sd;


/* SD Card hot plug detection pin */
#define SD_CHECK_PIN GET_PIN(D, 5)
#define USB_CHECK_PIN GET_PIN(B, 1)

static void _sdcard_mount(void)
{
    rt_device_t device;

    device = rt_device_find("sd0");
    if (device == RT_NULL)
    {
        mmcsd_wait_cd_changed(0);
        sdcard_change();
        mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
        device = rt_device_find("sd0");
    }
    if (device != RT_NULL)
    {
        if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
        {
            LOG_I("sd card mount to '/sdcard'");

            rt_sem_release(&g_sem_sd);
        }
        else
        {
            LOG_W("sd card mount to '/sdcard' failed!");
        }
    }
}

static void _sdcard_unmount(void)
{
    rt_thread_mdelay(200);
    dfs_unmount("/sdcard");
    LOG_I("Unmount \"/sdcard\"");

    mmcsd_wait_cd_changed(0);
    sdcard_change();
    mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
}

int USBCheckUp(int level)
{
    static int preLevel = 0;
    if(preLevel != level)
    {
        preLevel = level;
        return 1;
    }
    else
    {
        preLevel = level;
        return 0;
    }
}

int USBCheckDown(int level)
{
    static int preLevel = 1;
    if(preLevel != level)
    {
        preLevel = level;
        return 1;
    }
    else
    {
        preLevel = level;
        return 0;
    }
}

//static int sd_mount_mcu(int argc, char *argv[])
static void sd_mount_mcu(void *parameter)
{
    extern int stm_usbd_register(void);
    rt_device_t device;

    //usb拔出
    int level = rt_pin_read(USB_CHECK_PIN);

    if(USBCheckUp(level) == 1)
    {
        rt_kprintf("USB CheckcUp \n");
        _sdcard_unmount();
        rt_thread_mdelay(200);
        //以下为sd卡挂载在mcu上
         _sdcard_mount();
    }

    if(USBCheckDown(level) == 1)
    {
        rt_kprintf("USB CheckDown \n");
        _sdcard_unmount();
        rt_thread_mdelay(200);

        device = rt_device_find("sd0");
        if (device == NULL)
        {
            mmcsd_wait_cd_changed(0);
            sdcard_change();
            mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
            device = rt_device_find("sd0");
        }
        stm_usbd_register();
    }



//    rt_device_t device;
//
//    extern int stm_usbd_register(void);
//
//    device = rt_device_find("sd0");
//    if (device == NULL)
//    {
//        mmcsd_wait_cd_changed(0);
//        sdcard_change();
//        mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
//        device = rt_device_find("sd0");
//    }
//
//    stm_usbd_register();


//    return 0;
}



//static int sd_mount_mcu(int argc, char *argv[])
//{
//    //rt_uint8_t re_sd_check_pin = 1;
//    _sdcard_unmount();
//    rt_thread_mdelay(200);
//
////    rt_device_t device;
////
////    extern int stm_usbd_register(void);
////
////    device = rt_device_find("sd0");
////    if (device == NULL)
////    {
////        mmcsd_wait_cd_changed(0);
////        sdcard_change();
////        mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
////        device = rt_device_find("sd0");
////    }
////
////    stm_usbd_register();
//
//
//    //以下为sd卡挂载在mcu上
//    _sdcard_mount();
//}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(sd_mount_mcu, sdcard mount mcu);

//static int sd_mount_usb(int argc, char *argv[])
static void sd_mount_usb(int argc, char *argv[])
{
    //rt_uint8_t re_sd_check_pin = 1;
    _sdcard_unmount();
    rt_thread_mdelay(200);

    rt_device_t device;

    extern int stm_usbd_register(void);

    device = rt_device_find("sd0");
    if (device == NULL)
    {
        mmcsd_wait_cd_changed(0);
        sdcard_change();
        mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
        device = rt_device_find("sd0");
    }

    stm_usbd_register();


    //以下为sd卡挂载在mcu上
//    _sdcard_mount();
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(sd_mount_usb, sdcard mount usb);


int mount_init(void)
{
//    rt_thread_t tid;

//    tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
//                           2048, RT_THREAD_PRIORITY_MAX - 2, 20);

    rt_pin_mode(USB_CHECK_PIN, PIN_MODE_INPUT);


    rt_thread_t tid = rt_thread_create("sd_mount", sd_mount_mcu, RT_NULL, 2048, 23, 20);

    rt_kprintf("after create sd mount thread \n");

    if (tid != RT_NULL)
    {
        rt_kprintf("start sd_mount thread ok\n");
        rt_thread_startup(tid);
    }
    else
    {
        LOG_E("create sd_mount thread err!");
    }

    return RT_EOK;
}
INIT_APP_EXPORT(mount_init);

#define WIFI_RESET_PIN                 GET_PIN(C,  2)

int wifi_disable(void)
{
    rt_pin_mode(WIFI_RESET_PIN, PIN_MODE_OUTPUT);
    rt_pin_write(WIFI_RESET_PIN,PIN_LOW);
    return RT_EOK;
}
INIT_BOARD_EXPORT(wifi_disable);
